//
// Created by wormtql on 2020/11/18.
//

#include <map>
#include <string>
#include <cstring>
#include <fstream>
#include <queue>
#include <vector>
#include <sstream>
#include <iostream>
#include <set>
#include <cmath>
#include <algorithm>

using namespace std;

map<string, int> stats;

void parse_csv_line(char * line, vector<string> & data) {
    int state = 0;
    char buf[1000] = { 0 };
    int l = 0;

    int i = 0;
    while (line[i]) {
        if (state == 0) {
            if (line[i] == ',') {
                state = 1;
            } else {
                buf[l++] = line[i];
                i++;
            }
        } else if (state == 1) {
            buf[l] = 0;
            l = 0;
            data.push_back(string(buf));
//            printf("%s\n", buf);
            i++;
            state = 0;
        }
    }
    if (l != 0) {
//        printf("aaa\n");
        buf[l] = 0;
        data.emplace_back(buf);
    }
}

void load_stats(char * filename) {
    ifstream file(filename);
    char buf[1000];

    while (file.getline(buf, 1000)) {
        char name[100];
        int count;
        sscanf(buf, "%s%d", name, &count);
        stats[name] = count;
    }

    file.close();
}

int main(int argc, char * argv[]) {
#ifdef REMOTE
    printf("running remotely\n");
#endif

    if (argc < 4) {
        printf("choose nodes whose occurrence < threshold\n\n");

        printf("usage: app <stats.txt> <calltrace.csv> <output.csv> [threshold]\n");
        printf("stats.txt:      occurrence statistics\n");
        printf("calltrace.csv:  call trace to be represented\n");
        printf("output.csv:     output file\n");
        printf("threshold:      integer, default=0\n");
        return 0;
    }

    load_stats(argv[1]);
    printf("stats loaded\n");
    int threshold = 0;
    if (argc >= 5) {
        threshold = atoi(argv[4]);
    }
    printf("threshold = %d\n", threshold);

    ifstream trace_file(argv[2]);
    ofstream out_file(argv[3]);
    if (!out_file.is_open()) {
        printf("cannot open outfile\n");
        return 0;
    }
    char buf[10000] = { 0 };
    while (trace_file.getline(buf, 10000)) {
        vector<string> trace;
        parse_csv_line(buf, trace);

        vector<pair<string, int>> temp;
        for (int i = 0; i < trace.size(); i++) {
            temp.push_back(make_pair(trace[i], stats[trace[i]]));
        }

        sort(temp.begin(), temp.end(), [] (pair<string, int> & a, pair<string, int> & b) {
            return a.second < b.second;
        });

        if (temp.size() > 0) {
            out_file << temp[0].first;
            for (int i = 1; i < temp.size(); i++) {
                if (temp[i].second <= threshold) {
                    out_file << ",";
                    out_file << temp[i].first;
                } else {
                    break;
                }
            }
            out_file << endl;
        } else {
            out_file << endl;
        }
    }

    trace_file.close();
    out_file.close();
}